package cn.yeyiu.netonlinevideo.util;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.AndroidUtil;


/**
 * 源处理
 * 获取源
 */

public class SourceUtil {
    public static DataListViewModel dataModel;

    public static void initViewModel(DataListViewModel viewModel) {
        SourceUtil.dataModel = viewModel;
    }

    public static List<SourceData> getSourceList(Context context) {
        List<SourceData> sources = new ArrayList<>();

        List<String> parsers = AndroidUtil.getAssetList(context, "parsers");
        if (parsers == null) return null;

        for (String parFolder : parsers) {

            List<String> files = AndroidUtil.getAssetList(context, "parsers/" + parFolder);
            if (files == null) return null;

            for (String par : files) {
                String file = AndroidUtil.getAssetString(context, "parsers/" + parFolder + "/" + par);
                sources.add(JSONObject.parseObject(file, SourceData.class));
            }
        }
        return sources;
    }
}
